/*
 * JSFConsole - Lightweight JSF Application Monitor
 * Copyright (C) 2009  Grzegorz Bugaj
 * http://www.gregbugaj.com
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
*/
package com.gregbugaj.reflector.renderer;


import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import com.gregbugaj.jsfconsole.util.XMLUtil;
/**
 * Renders  given object into HTML Table
 * @author devil
 *
 */

public class HtmlRenderer extends AbstractRenderer{
	@Override
	protected  void emitObject(Node node, EmitContext context) throws Exception  {			
		c("<table class='class'>");
		c("<tbody>");
		c("<tr opath='"+node.getAttributes().getNamedItem("opath").getNodeValue()+"'>");
		c("<td valign='top'>");
		
		if(node.getAttributes().getNamedItem("class")==null){
		c("<h1>NULL</h1>");
		}else{
			c("<h1>"+node.getAttributes().getNamedItem("class").getNodeValue()+"</h1>");
		}
		c("</td>");
		c("<td>");
		
		NodeList fields = XMLUtil.extract("fields/*", node);
		NodeList methods = XMLUtil.extract("methods/*", node);
		

		
		c("<table class='fields' style='width:100%'>");
		c("<thead>");
		c("<tr class='fields-header'  onclick='toggleView(this);'><th colspan='2'><h2>Fields</h2></th></tr>");
		c("</thead>");	
		
		c("<tbody>");
		for(int i=0;i<fields.getLength();i++){
			c("<tr><td>");
			emit(fields.item(i), null);
			c("</td></tr>");
		}
		c("</tbody>");
		c("</table>");
		
		
		c("<table class='methods' style='width:100%'>");
		c("<thead>");
		c("<tr class='methods-header'  onclick='toggleView(this);'><th  colspan='2'><h2> Methods </h2></th></tr>");
		c("</thead>");
		c("<tbody>");
		for (int i = 0; i < methods.getLength(); i++) {
			emit(methods.item(i),null);
		}
		c("</tbody>");
		c("</table>");
		
		
		c("</td>");
		c("</tr>");
		c("</tbody>");
		c("</table>");
		c("\n");
	}
	
	@Override
	protected  void emitArrayField(Node node, EmitContext context)	throws Exception {
		c("<table class='collections'>");
			//c("<thead>");
			//c("<tr class='collections-header'  onclick='toggleView(this);'><th colspan='2'><h2>Array</h2></th></tr>");
			//c("</thead>");				
		c("<tbody>");
		c("<tr opath='"+node.getAttributes().getNamedItem("opath").getNodeValue()+"'>");
		
		c("<td>");
	
		c("<h2 class='black'>" 
		+ node.getAttributes().getNamedItem("type").getNodeValue()+"  " 
		+ node.getAttributes().getNamedItem("field").getNodeValue()
		+ "</h2>");
	
		c("</td>");
		c("</tr>");
		
		NodeList entrySet = XMLUtil.extract("./*", node);
		
			c("<tr style='width:100%'>");
			c("<td>  </td>");
			c("<td>");
			
			c("<table class='collections' style='width:100%'>");
			for (int i = 0; i < entrySet.getLength(); i++) {
				Node entry=entrySet.item(i);
				c("<tr opath='"+XMLUtil.attributeText(node, "opath")+"'>");
					c("<td valign='top' class='strong'> ");
						c("["+i+"]");
					c("</td>");
					c("<td>");
					emit(entry,null);
					c("</td>");
				c("</tr>");
			}						
			c("</table>");
		
		c("</tr>");
		c("</td>");
		
		c("</tbody>");
		c("</table>");
		c("\n");
				
	}
	
	@Override
	protected  void emitCollectionField(Node node, EmitContext context)	throws Exception {
		c("<table class='collections'>");
		/*
		c("<thead>");
		c("<tr class='collections-header'  onclick='toggleView(this);'><th colspan='2'><h2>Collections</h2></th></tr>");
		c("</thead>");
		*/
		c("<tbody>");	
		c("<tr>");
		c("<td>");
		
		c("<tr>");
		c("<td>");
		
		c("<h2 class='black'>" 
		+  XMLUtil.attributeText(node, "type")+"  " 
		+  XMLUtil.attributeText(node, "field")
		+ "</h2>");
	
		c("</td>");
		c("</tr>");
		
		NodeList entrySet = XMLUtil.extract("./*", node);	
		if (entrySet.getLength() > 0){
			c("<tr style='width:100%'>");
			c("<td>  </td>");
			c("<td>");
				c("<table class='methods' style='width:100%'>");
				/*
				c("<thead>");
				c("<tr class='methods-header'  onclick='toggleView(this);'><th  colspan='2'><h2> Collection </h2></th></tr>");
				c("</thead>");
				*/
			for (int i = 0; i < entrySet.getLength(); i++) {
				Node entry=entrySet.item(i);				
				c("<tr>");
					c("<td>");
						emit(entry,null);
					c("</td>");
				c("</tr>");
			}						
			c("</tbody>");
			c("</table>");
			c("\n");
		}
		
		c("</td>");
		c("</tr>");	
		
		c("</tbody>");
		c("</table>");
		c("\n");
	}
	
	@Override
	protected  void emitMethod(Node node, EmitContext context) throws Exception {
		//c("<table class='methods' style='width:100%'>");
		//c("<tbody>");
			c("<tr>");
			c("<td valign='top'>");
			c(node.getAttributes().getNamedItem("signature").getNodeValue());
			c("</td>");
			c("<td>");
			NodeList retValueList = XMLUtil.extract("./*", node);
			if(retValueList.getLength()>0){
				emit(retValueList.item(0),null); 
			}
			c("</td>");
			c("</tr>");
	//	c("</tbody>");
		//c("</table>");
		c("\n");
	}
	

	@Override
	protected void emitField(Node field, EmitContext context) throws Exception {
		c("<table class='fields' style='width:100%'>");
		c("<tr  style='width:100%' opath='"+XMLUtil.attributeText(field, "opath")+"'>");
		c("<td  style='width:33%;white-space:nowrap;'>");
		c(field.getAttributes().getNamedItem("type").getNodeValue());
		c("</td>");
			c("<td  style='width:33%;white-space:nowrap;'>");
			c(field.getAttributes().getNamedItem("name").getNodeValue());
			c("</td>");
		
		c("<td style='width:33%;white-space:nowrap;'>");
		c(field.getAttributes().getNamedItem("value").getNodeValue());
		c("</td>");
		c("</tr>");
		c("</table>");
	}


	@Override
	protected void emitMap(Node node, EmitContext context) throws Exception {
		c("<table class='collections'>");
		/*
		c("<thead>");
		c("<tr class='collections-header'  onclick='toggleView(this);'><th colspan='2'><h2>Collections</h2></th></tr>");
		c("</thead>");
		*/
		c("<tbody>");	
		c("<tr  style='width:100%' opath='"+XMLUtil.attributeText(node, "opath")+"'>");
		c("<td>");
		
		c("<tr>");
		c("<td>");
		
		c("<h2 class='black'>" 
		+  XMLUtil.attributeText(node, "type")+"  " 
		+  XMLUtil.attributeText(node, "field")
		+ "</h2>");
	
		c("</td>");
		c("</tr>");
		
		NodeList entrySet = XMLUtil.extract("./entry", node);	
		if (entrySet.getLength() > 0){
			c("<tr style='width:100%'>");
			c("<td>  </td>");
			c("<td>");
				c("<table class='methods' style='width:100%'>");
				/*
				c("<thead>");
				c("<tr class='methods-header'  onclick='toggleView(this);'><th  colspan='2'><h2> Collection </h2></th></tr>");
				c("</thead>");
				*/
			for (int i = 0; i < entrySet.getLength(); i++) {
				Node entry=entrySet.item(i);
				Node keyNode = XMLUtil.extract("./*[1]", entry).item(0);	
				Node valueNode = XMLUtil.extract("./*[2]", entry).item(0);	
		
				c("<tr>");
					c("<td>");
						emit(keyNode,null);
					c("</td>");
					c("<td>");
						emit(valueNode,null);
					c("</td>");
				c("</tr>");
			}						
			c("</tbody>");
			c("</table>");
			c("\n");
		}
		
		c("</td>");
		c("</tr>");	
		
		c("</tbody>");
		c("</table>");
		c("\n");
	}

	@Override
	protected void emitReference(Node node, EmitContext context) throws Exception {
		String value=XMLUtil.attributeText(node, "value");
		c("<a href='javascript:void(0)' onclick='focusRef()'>");
		c("Ref #"+ value);
		c("</a>");
	}

	
}
